/**
 * 日历构造器
 */

(function(){
	calendar = this;
	//节日数据
	this.festival={
			"2016/11/11":lang('festival.1'),
			"2016/11/24":lang('festival.2'),
			"2016/12/24":lang('festival.3'),
			"2016/12/25":lang('festival.4'),
			"2017/1/1":lang('festival.5'),
			"2017/1/27":lang('festival.6'),
			"2017/1/28":lang('festival.7'),
			"2017/2/11":lang('festival.8'),
			"2017/2/14":lang('festival9'),
			"2017/3/8":lang('festival.10'),
			"2017/3/15":lang('festival.11'),
			"2017/4/1":lang('festival.12'),
			"2017/4/4":lang('festival.13'),
			"2017/5/1":lang('festival.14'),
			"2017/5/4":lang('festival.15'),
			"2017/5/14":lang('festival.16'),
			"2017/5/30":lang('festival.17'),
			"2017/6/1":lang('festival.18'),
			"2017/6/18":lang('festival.19'),
			"2017/7/1":lang('festival.20'),
			"2017/8/1":lang('festival.21'),
			"2017/8/28":lang('festival.22'),
			"2017/9/10":lang('festival.23'),
			"2017/10/1":lang('festival.24'),
			"2017/10/4":lang('festival.25'),
			"2017/10/28":lang('festival.26'),
	};
	//放假数据
	this.holiday=[];

	//获取指定月份的时间戳范围，已补齐两端天数
	this.monthRange=function(year,month){
		//以当天零点的时间戳作为当天日期的标识
		var month_start = new Date(year+'/'+month+'/1');//包括
		month++;
		if(month>12){
			month = 1;
			year++;
		}
		var month_end = new Date(year+'/'+month+'/1'); //不包括，后面计算时要退一天处理
		
		var startFillDays = month_start.getDay()-1; //月头补齐天数
		startFillDays = (startFillDays==-1)?6:startFillDays;		

		var endFillDays = 7-month_end.getDay(); //月底补齐天数
		endFillDays = endFillDays>=6?endFillDays-7:endFillDays;
	
		var range = {};
		range.start = month_start.getTime()-startFillDays*86400000;
		range.end = month_end.getTime() + endFillDays*86400000; //已处理成月底最后一天的零点
		return range;
	};
	
	//获取指定月份的天数列表，包含该天的节日数据，已补齐两端天数
	this.getMonthData=function(year,month){
		
		var current = new Date();	
		var current_zero = new Date(current.getFullYear(),current.getMonth(),current.getDate()).getTime();
		
		//处理放假数据
		var holiday = {};
		for(var i in calendar.holiday){
			holiday[calendar.holiday[i]] = true;
		}
		
		//补充每个日期的相关信息
		var data=new Array()
		var range = calendar.monthRange(year, month);
		for(var t= range.start,i=0; t<=range.end;t=t+86400000){
			var dayInfo = {};
			var dayTime = new Date(t);
			dayInfo.yearNum = dayTime.getFullYear();
			dayInfo.monthNum = dayTime.getMonth()+1;
			dayInfo.dayNum = dayTime.getDate();
			dayInfo.date = dayInfo.yearNum+"/"+dayInfo.monthNum+"/"+dayInfo.dayNum;
			dayInfo.timestamp = parseInt(t/1000);
			dayInfo.isOverdue = (current_zero>t) ;
			dayInfo.isToday = (current_zero==t);
			dayInfo.isHoliday = holiday[dayInfo.date]?true:false;
			dayInfo.festival = calendar.festival[dayInfo.date]?calendar.festival[dayInfo.date]:"";
			data[i] = dayInfo;
			i++;
		}
		return data;
	};
	//获取当前月份及后5个月的月份表示
	this.getMonthArray=function(){
		var current = new Date();	
		var monthNum = current.getMonth()+1;
		var yearNum = current.getFullYear();
		
		var month = new Array();
		for(var i=0; i<6; i++){

			if(monthNum>12){
				monthNum -= 12;
				yearNum++;
			}
			
			month_item = {};
			month_item.month = monthNum;
			month_item.year = yearNum;
			month[i] = month_item;
			
			monthNum++;
		}
		return month;
	};
	this.test=function(){
		var t1 = new Date('2016/11/29 13:59:11');
		var t2 = new Date(1480399151000);
		console.log(t1);
		console.log(t2);
	};
})();